﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Besture.Bll;
using Besture.Model;
using Besture.Common.Definition;
using System.Data;
using Besture.Utils;
using System.Text.RegularExpressions;
using Besture.Common;

namespace Besture.UI.Service
{
    public partial class Risk : System.Web.UI.Page
    {
        //Banner图
        public ImageModel _BannerTop { get; set; }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                bindInvestLevel();
                bindInvestExp();
                bindInvestHouse();
                bindInvestFund();
                bindRiskReason();
                bindRiskYeild();
                bindRiskDirection();
                bindRiskDeepDirection();
                bindRiskFutureDirection();

                bindIndustry();
                bindIncomeSource();
                bindAlimonyUse();
                bindOnceInvest();
                bindAttentionInvest();
                bindInvestSource();
            }
            GetBanner();
        }

        #region 保存
        /// <summary>
        /// 风险测试
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="eventArgs"></param>
        protected void btnRiskTest_Click(object sender, EventArgs eventArgs)
        {
            RiskTestDac dac = new RiskTestDac();
            RiskTestModel model = new RiskTestModel();
            model.UID = 0;
            model.Mobile = txtMobilTest.Value.Trim();
            model.Name = txtNameTest.Value.Trim();
            if (string.IsNullOrEmpty(txtNameTest.Value.Trim()))
            {
                Fun.Alert(this.Page, "姓名不能为空");
                return;
            }
            if (string.IsNullOrEmpty(model.Mobile))
            {
                Fun.Alert(this.Page, "手机号码不能为空");
                return;
            }
            if (!Regex.IsMatch(model.Mobile, AppConfig.SMSRegex))
            {
                Fun.Alert(this.Page, "手机号码格式不正确");
                return;
            }

            model.InvestLevel = ddlInvestLevel.SelectedValue != "" ? Convert.ToInt32(ddlInvestLevel.SelectedValue) : 0;
            model.InvestExp = ddlInvestExp.SelectedValue != "" ? Convert.ToInt32(ddlInvestExp.SelectedValue) : 0;
            model.InvestHouse = ddlInvestHouse.SelectedValue != "" ? Convert.ToInt32(ddlInvestHouse.SelectedValue) : 0;
            model.InvestFund = ddlInvestFund.SelectedValue != "" ? Convert.ToInt32(ddlInvestFund.SelectedValue) : 0;
            model.RiskReason = ddlRiskReason.SelectedValue != "" ? Convert.ToInt32(ddlRiskReason.SelectedValue) : 0;
            model.RiskYeild = ddlRiskYeild.SelectedValue != "" ? Convert.ToInt32(ddlRiskYeild.SelectedValue) : 0;
            string allck = "";
            for (int i = 0; i < ckbRiskDirection.Items.Count; i++)
            {
                if (ckbRiskDirection.Items[i].Selected)
                    allck += ckbRiskDirection.Items[i].Value + ",";
            }
            allck = allck.TrimEnd(',');
            model.RiskDirection = allck;
            allck = "";
            for (int i = 0; i < ckbRiskDeepDirection.Items.Count; i++)
            {
                if (ckbRiskDeepDirection.Items[i].Selected)
                    allck += ckbRiskDeepDirection.Items[i].Value + ",";
            }
            allck = allck.TrimEnd(',');
            model.RiskDeepDirection = allck;
            allck = "";
            for (int i = 0; i < ckbRiskFutureDirection.Items.Count; i++)
            {
                if (ckbRiskFutureDirection.Items[i].Selected)
                    allck += ckbRiskFutureDirection.Items[i].Value + ",";
            }
            allck = allck.TrimEnd(',');
            model.RiskFutureDirection = allck;
            model.IsDeleted = 0;
            model.CreateTime = DateTime.Now;
            model.UpdateTime = DateTime.Now;
            dac.Add(model);

            Fun.Alert("保存成功","Risk.aspx");
        }

        /// <summary>
        /// 资产配置
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="eventArgs"></param>
        protected void btnSubmit_Click(object sender, EventArgs eventArgs)
        {
            RiskConfigDac dac = new RiskConfigDac();
            RiskConfigModel model = null;
            //添加
            model = new RiskConfigModel();
            model.UID = 0;
            model.Mobile = txtMobile.Value.Trim();
            model.Name = txtName.Value.Trim();
            if (string.IsNullOrEmpty(txtName.Value.Trim()))
            {
                Fun.Alert(this.Page, "姓名不能为空", "ShowRisk(1);");
                return;
            }
            if (string.IsNullOrEmpty(model.Mobile))
            {
                Fun.Alert(this.Page, "手机号码不能为空", "ShowRisk(1);");
                return;
            }
            if (!Regex.IsMatch(model.Mobile, AppConfig.SMSRegex))
            {
                Fun.Alert(this.Page, "手机号码格式不正确", "ShowRisk(1);");
                return;
            }
            if (string.IsNullOrEmpty(txtStartFund.Value.Trim()))
            {
                Fun.Alert(this.Page, "起始可用投资金额不能为空", "ShowRisk(1);");
                return;
            }
            if (string.IsNullOrEmpty(txtYearInvest.Value.Trim()))
            {
                Fun.Alert(this.Page, "年结余投资保障比不能为空", "ShowRisk(1);");
                return;
            }

            if (string.IsNullOrEmpty(txtEndTime.Value.Trim()))
            {
                Fun.Alert(this.Page, "目标截止日期不能为空", "ShowRisk(1);");
                return;
            }
            if (string.IsNullOrEmpty(txtDescription.Value.Trim()))
            {
                Fun.Alert(this.Page, "理财目标描述不能为空", "ShowRisk(1);");
                return;
            }

            if (string.IsNullOrEmpty(txtTargetFund.Value.Trim()))
            {
                Fun.Alert(this.Page, "理财目标金额不能为空", "ShowRisk(1);");
                return;
            }
            if (string.IsNullOrEmpty(txtTargetDescription.Value.Trim()))
            {
                Fun.Alert(this.Page, "目标描述不能为空", "ShowRisk(1);");
                return;
            }

            model.HealthStatus = txtHealthStatus.Value.Trim();
            model.Preference = txtPreference.Value.Trim();
            model.Industry = ddlIndustry.SelectedValue != "" ? Convert.ToInt32(ddlIndustry.SelectedValue) : 0;
            model.IsHasCom = rbtnIsHasCom.SelectedValue != "" ? Convert.ToInt32(rbtnIsHasCom.SelectedValue) : 0;
            string allck = "";
            for (int i = 0; i < ckbIncomeSource.Items.Count; i++)
            {
                if (ckbIncomeSource.Items[i].Selected)
                    allck += ckbIncomeSource.Items[i].Value + ",";
            }
            allck = allck.TrimEnd(',');
            model.IncomeSource = allck;
            model.Alimony = txtAlimony.Value.Trim();
            model.AlimonyUse = ddlAlimonyUse.SelectedValue != "" ? Convert.ToInt32(ddlAlimonyUse.SelectedValue) : 0;
            allck = "";
            for (int i = 0; i < ckbOnceInvest.Items.Count; i++)
            {
                if (ckbOnceInvest.Items[i].Selected)
                    allck += ckbOnceInvest.Items[i].Value + ",";
            }
            allck = allck.TrimEnd(',');
            model.OnceInvest = allck;
            allck = "";
            for (int i = 0; i < ckbAttentionInvest.Items.Count; i++)
            {
                if (ckbAttentionInvest.Items[i].Selected)
                    allck += ckbAttentionInvest.Items[i].Value + ",";
            }
            allck = allck.TrimEnd(',');
            model.AttentionInvest = allck;
            allck = "";
            for (int i = 0; i < ckbInvestSource.Items.Count; i++)
            {
                if (ckbInvestSource.Items[i].Selected)
                    allck += ckbInvestSource.Items[i].Value + ",";
            }
            allck = allck.TrimEnd(',');
            model.InvestSource = allck;
            model.StartFund = txtStartFund.Value.Trim();
            model.YearInvest = txtYearInvest.Value.Trim();
            model.EndTime = txtEndTime.Value.Trim() != "" ? Convert.ToDateTime(txtEndTime.Value.Trim()) : DateTime.Now;
            model.Description = txtDescription.Value.Trim();
            model.TargetFund = txtTargetFund.Value.Trim();
            model.TargetDescription = txtTargetDescription.Value.Trim();
            model.YearIn = txtYearIn.Value.Trim();
            model.YearOut = txtYearOut.Value.Trim();
            model.MySalaryIn = txtMySalaryIn.Value.Trim();
            model.MySalaryOut = txtMySalaryOut.Value.Trim();
            model.WifeSalaryIn = txtWifeSalaryIn.Value.Trim();
            model.MedicalOut = txtMedicalOut.Value.Trim();
            model.BonusIn = txtBonusIn.Value.Trim();
            model.EducationOut = txtEducationOut.Value.Trim();
            model.InterestIn = txtInterestIn.Value.Trim();
            model.SafeOut = txtSafeOut.Value.Trim();
            model.RentIn = txtRentIn.Value.Trim();
            model.OtherOut = txtOtherOut.Value.Trim();
            model.OtherIn = txtOtherIn.Value.Trim();
            model.YearOutRate = txtYearOutRate.Value.Trim();
            model.YearInRate = txtYearInRate.Value.Trim();
            model.Assets = txtAssets.Value.Trim();
            model.Liabilities = txtLiabilities.Value.Trim();
            model.Cash = txtCash.Value.Trim();
            model.FundLoans = txtFundLoans.Value.Trim();
            model.FixedIn = txtFixedIn.Value.Trim();
            model.PersonalLoan = txtPersonalLoan.Value.Trim();
            model.FloatingIn = txtFloatingIn.Value.Trim();
            model.HousingLoans = txtHousingLoans.Value.Trim();
            model.RealEstate = txtRealEstate.Value.Trim();
            model.OtherDebt = txtOtherDebt.Value.Trim();
            model.NoRealEstate = txtNoRealEstate.Value.Trim();
            model.Insurance = txtInsurance.Value.Trim();
            model.OtherAssets = txtOtherAssets.Value.Trim();
            model.AssetsScale = txtAssetsScale.Value.Trim();
            model.CreateTime = DateTime.Now;
            model.UpdateTime = DateTime.Now;
            model.IsDeleted = 0;


            dac.Add(model);
            Fun.Alert("保存成功", "Risk.aspx");
        }

        #endregion

        #region 获取Banner
        /// <summary>
        /// 获取Banner
        /// </summary>
        public void GetBanner()
        {
            ImageDac theDac = new ImageDac();
            DataTable dt = theDac.GetDataTable(Constants.DICTIONARY_TYPE_AboutServiceClass);

            rpFinancialList.DataSource = dt;
            rpFinancialList.DataBind();


            ImageDac imageDac = new ImageDac();
            ImageModel imgModel = imageDac.GetDataTableByCode(Constants.DICTIONARY_CODE_BannerService, Constants.DICTIONARY_TYPE_IMAGES);
            if (imgModel != null)
                _BannerTop = imgModel;
            else
                _BannerTop = new ImageModel();
        }
        #endregion

        #region 风险测试 属性值绑定
        /// <summary>
        ///绑定投资的了解程度
        /// </summary>
        public void bindInvestLevel()
        {
            DictionaryDac dac = new DictionaryDac();
            DataTable dt = dac.GetDataTableByType(Constants.DICTIONARY_TYPE_您对投资的了解程度);
            this.ddlInvestLevel.Items.Add(new ListItem("请选择", ""));
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                this.ddlInvestLevel.Items.Add(new ListItem(dt.Rows[i]["Name"].ToString(), dt.Rows[i]["Code"].ToString()));
            }
        }
        /// <summary>
        ///绑定投资经验
        /// </summary>
        public void bindInvestExp()
        {
            DictionaryDac dac = new DictionaryDac();
            DataTable dt = dac.GetDataTableByType(Constants.DICTIONARY_TYPE_您的投资经验);
            this.ddlInvestExp.Items.Add(new ListItem("请选择", ""));
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                this.ddlInvestExp.Items.Add(new ListItem(dt.Rows[i]["Name"].ToString(), dt.Rows[i]["Code"].ToString()));
            }
        }
        /// <summary>
        ///绑定置业情况
        /// </summary>
        public void bindInvestHouse()
        {
            DictionaryDac dac = new DictionaryDac();
            DataTable dt = dac.GetDataTableByType(Constants.DICTIONARY_TYPE_您的置业情况);
            this.ddlInvestHouse.Items.Add(new ListItem("请选择", ""));
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                this.ddlInvestHouse.Items.Add(new ListItem(dt.Rows[i]["Name"].ToString(), dt.Rows[i]["Code"].ToString()));
            }
        }
        /// <summary>
        ///绑定可投资金额
        /// </summary>
        public void bindInvestFund()
        {
            DictionaryDac dac = new DictionaryDac();
            DataTable dt = dac.GetDataTableByType(Constants.DICTIONARY_TYPE_您目前可投资金额);
            this.ddlInvestFund.Items.Add(new ListItem("请选择", ""));
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                this.ddlInvestFund.Items.Add(new ListItem(dt.Rows[i]["Name"].ToString(), dt.Rows[i]["Code"].ToString()));
            }
        }
        /// <summary>
        ///绑定投资首选因素
        /// </summary>
        public void bindRiskReason()
        {
            DictionaryDac dac = new DictionaryDac();
            DataTable dt = dac.GetDataTableByType(Constants.DICTIONARY_TYPE_您的投资首选因素);
            this.ddlRiskReason.Items.Add(new ListItem("请选择", ""));
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                this.ddlRiskReason.Items.Add(new ListItem(dt.Rows[i]["Name"].ToString(), dt.Rows[i]["Code"].ToString()));
            }
        }
        /// <summary>
        ///绑定年化收益率
        /// </summary>
        public void bindRiskYeild()
        {
            DictionaryDac dac = new DictionaryDac();
            DataTable dt = dac.GetDataTableByType(Constants.DICTIONARY_TYPE_您期望的年化收益率);
            this.ddlRiskYeild.Items.Add(new ListItem("请选择", ""));
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                this.ddlRiskYeild.Items.Add(new ListItem(dt.Rows[i]["Name"].ToString(), dt.Rows[i]["Code"].ToString()));
            }
        }
        /// <summary>
        ///绑定主要投资方向
        /// </summary>
        public void bindRiskDirection()
        {
            DictionaryDac dac = new DictionaryDac();
            DataTable dt = dac.GetDataTableByType(Constants.DICTIONARY_TYPE_您目前的主要投资方向);
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                this.ckbRiskDirection.Items.Add(new ListItem(dt.Rows[i]["Name"].ToString(), dt.Rows[i]["Code"].ToString()));
            }
        }
        /// <summary>
        ///绑定了解的投资方向
        /// </summary>
        public void bindRiskDeepDirection()
        {
            DictionaryDac dac = new DictionaryDac();
            DataTable dt = dac.GetDataTableByType(Constants.DICTIONARY_TYPE_您打算深入了解的投资方向);
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                this.ckbRiskDeepDirection.Items.Add(new ListItem(dt.Rows[i]["Name"].ToString(), dt.Rows[i]["Code"].ToString()));
            }
        }
        /// <summary>
        ///绑定避免的投资方向
        /// </summary>
        public void bindRiskFutureDirection()
        {
            DictionaryDac dac = new DictionaryDac();
            DataTable dt = dac.GetDataTableByType(Constants.DICTIONARY_TYPE_您未来避免的投资方向);
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                this.ckbRiskFutureDirection.Items.Add(new ListItem(dt.Rows[i]["Name"].ToString(), dt.Rows[i]["Code"].ToString()));
            }
        }
        #endregion

        #region 资产配置 属性值绑定

        /// <summary>
        ///绑定
        /// </summary>
        public void bindIndustry()
        {
            DictionaryDac dac = new DictionaryDac();
            DataTable dt = dac.GetDataTableByType(Constants.DICTIONARY_TYPE_从事行业);
            this.ddlIndustry.Items.Add(new ListItem("请选择", ""));
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                this.ddlIndustry.Items.Add(new ListItem(dt.Rows[i]["Name"].ToString(), dt.Rows[i]["Code"].ToString()));
            }
        }
        /// <summary>
        ///绑定
        /// </summary>
        public void bindIncomeSource()
        {
            DictionaryDac dac = new DictionaryDac();
            DataTable dt = dac.GetDataTableByType(Constants.DICTIONARY_TYPE_收入来源);
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                this.ckbIncomeSource.Items.Add(new ListItem(dt.Rows[i]["Name"].ToString(), dt.Rows[i]["Code"].ToString()));
            }
        }
        /// <summary>
        ///绑定
        /// </summary>
        public void bindAlimonyUse()
        {
            DictionaryDac dac = new DictionaryDac();
            DataTable dt = dac.GetDataTableByType(Constants.DICTIONARY_TYPE_赡养费用途);
            this.ddlAlimonyUse.Items.Add(new ListItem("请选择", ""));
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                this.ddlAlimonyUse.Items.Add(new ListItem(dt.Rows[i]["Name"].ToString(), dt.Rows[i]["Code"].ToString()));
            }
        }
        /// <summary>
        ///绑定
        /// </summary>
        public void bindOnceInvest()
        {
            DictionaryDac dac = new DictionaryDac();
            DataTable dt = dac.GetDataTableByType(Constants.DICTIONARY_TYPE_曾经投资产品);
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                this.ckbOnceInvest.Items.Add(new ListItem(dt.Rows[i]["Name"].ToString(), dt.Rows[i]["Code"].ToString()));
            }
        }
        /// <summary>
        ///绑定
        /// </summary>
        public void bindAttentionInvest()
        {
            DictionaryDac dac = new DictionaryDac();
            DataTable dt = dac.GetDataTableByType(Constants.DICTIONARY_TYPE_投资时关注的问题);
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                this.ckbAttentionInvest.Items.Add(new ListItem(dt.Rows[i]["Name"].ToString(), dt.Rows[i]["Code"].ToString()));
            }
        }
        /// <summary>
        ///绑定
        /// </summary>
        public void bindInvestSource()
        {
            DictionaryDac dac = new DictionaryDac();
            DataTable dt = dac.GetDataTableByType(Constants.DICTIONARY_TYPE_投资资讯主要来源);
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                this.ckbInvestSource.Items.Add(new ListItem(dt.Rows[i]["Name"].ToString(), dt.Rows[i]["Code"].ToString()));
            }
        }

        #endregion
    }
}